package com.gowants.pems.util;

import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * <文件上传>
 *
 * @ClassName UploadUtil
 * @remake create by GJR 2019/6/15 11:34
 */
public class UploadUtil
{

    /**
     * <MultipartFile 转换 File>
     *
     * @param
     * @return
     * @remake create by GJR 2019/6/15 11:35
     */
    public static File toFile(MultipartFile file) throws Exception
    {
        File targetFile = null;
        try
        {
            file.transferTo(targetFile);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return targetFile;
    }

    /**
     * <保存文件>
     *
     * @param
     * @return
     * @remake create by GJR 2019/6/15 11:51
     */
    public static void saveFileFromInputStream(InputStream stream, String path) throws IOException
    {
        // 自动创建文件夹
        File file = new File(path);
        File fileParent = file.getParentFile();

        if(!fileParent.exists())
        {
            fileParent.mkdirs();
        }

        // 保存文件
        FileOutputStream fs = new FileOutputStream(path);
        byte[] buffer = new byte[1024*1024];
        int bytesum = 0;
        int byteread = 0;
        while ((byteread = stream.read(buffer)) != -1)
        {
            bytesum += byteread;
            fs.write(buffer,0, byteread);
            fs.flush();
        }
        fs.close();
        stream.close();
    }

    /**
     * <获取文件名>
     *
     * @param
     * @return
     * @remake create by GJR 2019/6/15 11:53
     */
    public static String getFileName(MultipartFile file)
    {
        return file.getOriginalFilename();
    }

    /**
     * <查询目录下所有文件>
     *
     * @param
     * @return
     * @remake create by GJR 2019/6/17 14:58
     */
    public static void getFile(String path, int deep, List<String> resultList){

        // 获得指定文件对象
        File file = new File(path);
        // 获得该文件夹内的所有文件
        File[] array = file.listFiles();

        if(null == array)
        {
            return;
        }

        for(int i=0;i<array.length;i++)
        {
            if(array[i].isFile())//如果是文件
            {
                for (int j = 0; j < deep; j++)//输出前置空格
                    System.out.println(" ");
                // 只输出文件名字
//                    resultList.add(array[i].getName());
                // 同样输出当前文件的完整路径   大家可以去掉注释 测试一下
                resultList.add(array[i].getPath().replace("\\", "/"));
            }
            else if(array[i].isDirectory())//如果是文件夹
            {
                for (int j = 0; j < deep; j++)//输出前置空格
//                System.out.println( array[i].getName());
                //System.out.println(array[i].getPath());
                //文件夹需要调用递归 ，深度+1
                getFile(array[i].getPath(),deep+1, resultList);
            }
        }
    }

    public static List<String> getAllFiles(String path, int deep)
    {
        List<String> resultList = new ArrayList<String>();
        getFile(path, 0, resultList);
//        ComparatorList comparatorList = new ComparatorList();
//        Collections.sort(resultList, comparatorList);

        return resultList;
    }

    /**
     * <测试方法>
     *
     * @param
     * @return
     * @remake create by GJR 2019/6/15 11:56
     */
    public static void main(String[] args) {

    }

}